package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * @Title: GetCookie
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/25 10:10
 * Cookie 默认时常  浏览器关闭就没有了
 */
@WebServlet("/get")
public class GetCookie  extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       // 获取cookies
        Cookie[] cookies = req.getCookies();
        if(cookies != null) {
            for (Cookie c: cookies) {
                // cookie的key 和 value
                String name = c.getName();
                String value = c.getValue();
                // 普通获取
                System.out.println(name+":"+value);
                // 解密获取
                System.out.println(name+":"+ URLDecoder.decode(value,"gbk"));
            }
        }
        resp.getWriter().write("success!");
    }
}
